package com.kehutong.employee.service;


import com.kehutong.employee.dao.EmployeeDao;
import com.kehutong.employee.entity.Employee;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.Condition;
import org.coraframework.orm.jdbc.execute.QueryCondition;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.Map;


@WebService("/basic/employee/service/api")
public class EmployeeServiceApi {
    static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private EmployeeDao employeeDao;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        JSONArray departments = jsonObject.getJSONArray("department");

        FindPage<Employee> find = jdbcSession.findPage(Employee.class)
                .in(jsonObject, "id")
                .eq(jsonObject, "name").and();

        if(Objects.nonEmpty(departments)){
            find.markBegin();
            for (Object item: departments) {
                find.like("department",String.valueOf(item)).or();
            }
            find.markEnd().and();
        }
        find.eq("deleted", false);
        Page<Employee> page = find.page(jsonObject).exe();

        return page;
    }
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Employee item) throws Exception {
        Employee employee = jdbcSession.findOne(Employee.class)
                .eq("userid", item.getUserid())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(employee)){
            return Result.error(0,"员工账号不能重复");
        }


        QueryCondition query = Condition.createQuery(false);
        Map<String,Object> rs = employeeDao.getMaxSort(query);
        item.setSort(Long.valueOf(rs.get("sort").toString()));

        jdbcSession.insert(item);
        return Result.success(item);
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(Employee item) throws Exception {
        Employee employee = jdbcSession.findOne(Employee.class)
                .eq("userid", item.getUserid())
                .noEq("id", item.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(employee)){
            return Result.error(0,"员工账号不能重复");
        }
        jdbcSession.updateById(item);
        return Result.success(item);
    }
    @ReqMapping("/get")
    public Object get(JSONObject jsonObject) throws Exception {
        String userId=jsonObject.getString("userId");  //t_user id
        Employee employee = jdbcSession.findById(Employee.class,userId);
        return employee;
    }
    @ReqMapping("/delete")
    public Object delete(Employee item) throws Exception {
        item.setDeleted(true);
        return jdbcSession.updateById(item);
    }
}
